package com.openbravo.pos.printer;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

import com.openbravo.pos.printer.escpos.PrinterWritter;

public class PrinterWritterLan extends PrinterWritter{

	String addr;
	int port;
	private OutputStream m_out;
	
	public PrinterWritterLan(String addr, int point) {
		super();
		this.addr = addr;
		this.port = point;
	}

	@Override
	protected void internalWrite(byte[] data) {
		 try {  
	            if (m_out == null) {
	            	Socket client=new java.net.Socket();
	            	client.connect(new InetSocketAddress(addr,port),1000); // 创建一个 socket
	            	m_out = client.getOutputStream();// 创建输入输出数据流
	            }
	            m_out.write(data);
	        } catch (IOException e) {
	            System.err.println(e);
	        } 
		
	}

	protected void internalFlush() {
        try {  
            if (m_out != null) {
                m_out.flush();
                m_out.close();
                m_out = null;
            }
        } catch (IOException e) {
            System.err.println(e);
        }    
    }
    
    protected void internalClose() {
        try {  
            if (m_out != null) {
                m_out.flush();
                m_out.close();
                m_out = null;
            }
        } catch (IOException e) {
            System.err.println(e);
        }    
    }    

}
